<?php
namespace Home\Model;
use Think\Model;

class OrderModel extends Model {

    public function search($pageSize = 20) {
        /************************************************************ 搜索 ***************************************/
        $where = array();
        $where['client_id'] = array('eq', I('get.id'));
        /********************************************************** 翻页 ***************************************/
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $data['page'] = $page->show();
        /******************************************************* 取数据 *****************************************/
        $data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow . ',' . $page->listRows)->select();
        return $data;
    }

    // 添加前
    protected function _before_insert(&$data, $option) {
        $time = I('post.atime');
        //var_dump($time);
        $data['atime'] = strtotime($time);
    }

    // 修改前
    protected function _before_update(&$data, $option) {
        $time = I('post.atime');
        //var_dump($time);
        $data['atime'] = strtotime($time);
    }

    // 删除前
    protected function _before_delete($option) {
        if (is_array($option['where']['id'])) {
            $this->error = '不支持批量删除';
            return FALSE;
        }
    }

    /**************************************** 其他方法 ******************************************* */

    //取出订单贡献
    public function analysisContribute($pageSize = 20) {
        /**************************************************** 搜索 *************************************** */
        $where = array();
        if ($client_name = I('get.client_name'))
            $where['client_name'] = array('like', "%$client_name%");
        if ($year = I('get.year')) {
            $year = yearstr($year);
            $where['atime'] = array('gt', $year["first"]);
            $where['atime'] = array('lt', $year["last"]);
        }
        //原生sql语句写where
        if($client_name = I('get.client_name'))
			$where1 = 'where client_name like '."'%$client_name%'";
		if($year = I('get.year')){
                    $year=yearstr($year);
                    $where['atime'] = array('gt', $year['first']);
                    $where['atime'] = array('lt', $year['last']);
                    $where1= 'where atime>'.$year['first'].' and atime<'.$year['last'];
                }
        /************************************************ 翻页 ***************************************/
        $count = $this->where($where)->group('client_id')->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $data['page'] = $page->show();
        /***************************************** 取数据 *********************************************/
        
        //$data['data'] = $oModel->field('id,client_id,')->where($where)->group('client_id')->limit($page->firstRow.','.$page->listRows)->select();
        $sql = 'select client_id,client_name,SUM(money) as money from crm_order ' . $where1 . '    group by client_id limit ' . $page->firstRow . ',' . $page->listRows;
        $data['data'] = $this->query($sql);
        return $data;
    }

}
